//
//  LoanInterestView.m
//  SZBank
//
//  Created by rimi on 16/7/23.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "LoanInterestView.h"
#import "InterestTableViewCell.h"
#import "CalculateView.h"
@interface LoanInterestView()<UITableViewDataSource>
{
    CGFloat frameWidth;/**  自身宽度 */
    CGFloat frameHeight;/**  自身高度 */
    NSArray *sourceArray;
    UITextField *rentalTextField;/**  计算器文本框(贷款总额度) */
     UITextField *monthTextField;/**  计算器文本框(还款总期数) */
     UITextField *rateTextField;/**  计算器文本框(贷款利率) */
}
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) CalculateView *calculate;
@end
@implementation LoanInterestView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        /**  利率调整表图片 */
        UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(frameWidth * 0.02, frameHeight * 0.01, frameWidth * 0.55, frameHeight * 0.9)];
        leftImageView.image = [LocalDataManager getImageWithSourceOfPath:@"银行业务_贷款利率"];
        /**  计算器图片 */
        UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(frameWidth * 0.6, frameHeight * 0.077, frameWidth * 0.4, frameHeight * 0.4)];
        rightImageView.image = [LocalDataManager getImageWithSourceOfPath:@"银行业务_按揭贷款计算器"];
        [self addSubview:self.tableView];
        [self addSubview:leftImageView];
        [self addSubview:rightImageView];
        [self initUserInterface];
    }
    return self;
}

- (void)initUserInterface{
    /**  下拉按钮 */
    ButtonAndTableView *dateButton = [[ButtonAndTableView alloc] initWithFrame:CGRectMake(frameWidth * 0.02, frameHeight * 0.065, frameWidth * 0.2, frameHeight * 0.05) dataSource:@{@"title":@"2013-08-09",@"list":@[@"2013-08-09"]} backImageName:@"银行业务_按揭贷款_选择框" selectImageName:@"银行业务_按揭贷款_选择框"];
    [self addSubview:dateButton];
    /**  重置按钮 */
    UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake(frameWidth * 0.61, frameHeight * 0.5, frameWidth * 0.08, frameHeight * 0.05)];
    [resetBtn setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"银行业务_重置"] forState:UIControlStateNormal];
    resetBtn.titleLabel.font = [UIFont systemFontOfSize:24];
    [resetBtn setTitle:@"重置" forState:UIControlStateNormal];
    [resetBtn addTarget:self action:@selector(action_resetBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:resetBtn];
    /**  计算按钮 */
    UIButton *calculateBtn = [[UIButton alloc] initWithFrame:CGRectMake(frameWidth * 0.7, frameHeight * 0.5, frameWidth * 0.08, frameHeight * 0.05)];
    [calculateBtn setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"银行业务_计算"] forState:UIControlStateNormal];
    calculateBtn.titleLabel.font = [UIFont systemFontOfSize:24];
    [calculateBtn setTitle:@"计算" forState:UIControlStateNormal];
    [calculateBtn addTarget:self action:@selector(action_calculateBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:calculateBtn];
    /**  计算器文本框 */
    rentalTextField = [[UITextField alloc] initWithFrame:CGRectMake(frameWidth * 0.78, frameHeight * 0.14, frameWidth * 0.17, frameHeight * 0.05)];
    [rentalTextField setBackground:[LocalDataManager getImageWithSourceOfPath:@"银行业务_选中_菜单按钮"]];
    monthTextField = [[UITextField alloc] initWithFrame:CGRectMake(frameWidth * 0.78, frameHeight * 0.21, frameWidth * 0.17, frameHeight * 0.05)];
    [monthTextField setBackground:[LocalDataManager getImageWithSourceOfPath:@"银行业务_选中_菜单按钮"]];
    rateTextField = [[UITextField alloc] initWithFrame:CGRectMake(frameWidth * 0.78, frameHeight * 0.35, frameWidth * 0.17, frameHeight * 0.05)];
    [rateTextField setBackground:[LocalDataManager getImageWithSourceOfPath:@"银行业务_选中_菜单按钮"]];
    [self addSubview:rentalTextField];
    [self addSubview:monthTextField];
    [self addSubview:rateTextField];
    /**  计算器下拉按钮 */
    ButtonAndTableView *button = [[ButtonAndTableView alloc] initWithFrame:CGRectMake(frameWidth * 0.78, frameHeight * 0.28, frameWidth * 0.17, frameHeight * 0.05) dataSource:@{@"title":@"等额本息还款",@"list":@[@"等额本息还款",@"等额本金还款"]} backImageName:@"银行业务_按揭贷款_选择框" selectImageName:@"银行业务_按揭贷款_选择框"];
    [self addSubview:button];
}

- (void)showCalculateResult{
    
}
#pragma mark -- 点击事件
- (void)action_resetBtn{
    rentalTextField.text = @"";
    monthTextField.text = @"";
    rateTextField.text = @"";
    [self.calculate removeFromSuperview];
}

- (void)action_calculateBtn{
    NSArray *arrayData = @[rentalTextField.text,monthTextField.text,rateTextField.text];
    [self.calculate removeFromSuperview];
    NSString *str = monthTextField.text;
    self.calculate = [[CalculateView alloc] initWithFrame:CGRectMake(frameWidth * 0.6, frameHeight * 0.57, frameWidth * 0.4, frameHeight * 0.31) withIndex:[str integerValue] withArray:arrayData];
    [self addSubview:self.calculate];
}

#pragma mark -- tableView代理方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *data;
    NSArray *array1 = @[@"5.850",@"4.875",@"5.600",@"4.667"];
    NSArray *array2 = @[@"6.310",@"5.258",@"6.000",@"5.258"];
    NSArray *array3 = @[@"6.400",@"5.333",@"6.150",@"5.125"];
    NSArray *array4 = @[@"6.650",@"5.542",@"6.400",@"5.333"];
    NSArray *array5 = @[@"6.800",@"5.667",@"6.550",@"5.458"];
    sourceArray = @[array1,array2,array3,array4,array5];
    InterestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (indexPath.row == 4) {
        data = sourceArray[0];
    }
    else if (indexPath.row == 5) {
        data = sourceArray[1];
    }
    else if (indexPath.row == 7) {
        data = sourceArray[2];
    }
    else if (indexPath.row == 8) {
        data = sourceArray[3];
    }
    else if (indexPath.row == 9) {
        data = sourceArray[4];
    }
    if (indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 7 || indexPath.row == 8 || indexPath.row == 9) {
    for (int i = 0; i < 4; i ++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(frameWidth * (i * 0.097 + 0.176), frameHeight * 0.01, frameWidth * 0.07, frameHeight * 0.038)];
        [self getLabel:label andText:data[i]];
        [cell.contentView addSubview:label];
        }
    }
    if (indexPath.row == 10) {
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(frameWidth * 0.17, frameHeight * 0.01, frameWidth * 0.18, frameHeight * 0.038)];
        label1.text = @"以再贴现利率为下限加点确定";
        label1.font = [UIFont systemFontOfSize:15];
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(frameWidth * 0.364, frameHeight * 0.01, frameWidth * 0.18, frameHeight * 0.038)];
        label2.font = [UIFont systemFontOfSize:15];
        label2.text = @"以再贴现利率为下限加点确定";
        [cell.contentView addSubview:label1];
        [cell.contentView addSubview:label2];
    }
    NSArray *nomalArray = @[@[@"4.200",@"4.700"],@[@"4.200",@"4.700"]];
    NSArray *executeArray;
    if (indexPath.row == 13 || indexPath.row == 14) {
        if (indexPath.row == 13) {
            executeArray = nomalArray[0];
        }
        else{
           executeArray = nomalArray[1];
        }
        for (int k = 0; k < 2; k ++) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(frameWidth * (k * 0.2 + 0.223), frameHeight * 0.01, frameWidth * 0.07, frameHeight * 0.038)];
            [self getLabel:label andText:executeArray[k]];
            [cell.contentView addSubview:label];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 15;
}

- (void)getLabel:(UILabel *)label andText:(NSString *)text{
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = text;
    label.font = [UIFont systemFontOfSize:22];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(frameWidth * 0.02, frameHeight * 0.123, frameWidth * 0.55, frameHeight * 0.788)];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.rowHeight = frameHeight * 0.788 / 15;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[InterestTableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}
@end
